import cv2
import os
import time
#切片截图的


# 视频路径
video_path = r'E:\vedio'
# 保存图片的路径
savedpath = r'E:\demo'

video_list = os.listdir(video_path)

# 保存图片的帧率间隔
count = 5
i = 0



for index, video_name in enumerate(video_list):
    j = 0
    video_path_ = os.path.join(video_path, video_name)
    # 某个视频帧保存路径
    save_path_ = os.path.join(savedpath, video_name.split('.')[0])
    if os.path.exists(save_path_):
        pass
    else:
        os.mkdir(save_path_)

    # 开始读视频

    videoCapture = cv2.VideoCapture(video_path_)

    print("正在处理第{}个视频，总共{}个视频".format(index + 1, len(video_list)))

    while True:

        success, frame = videoCapture.read()

        i += 1

        if (i % count == 0):
            # 保存图片

            j += 1

            savedname = video_name[:-4] + str(i).zfill(4) + '.jpg'

            cv2.imwrite(os.path.join(save_path_, savedname), frame)

            print('image of %s is saved' % (savedname))

        if not success:
            print('video is all read')

            break

    videoCapture.release()

    time.sleep(5)
